const {db, genid} = require('../db/index')
const {v4: uuidv4} = require('uuid')

exports.login = async (req, res) => {
    let {account, password} = req.body;
    const sql = 'select * from admin where account = ? and password = ?'
    let {err, results} = await db.async.query(sql, [account, password])
    if(err == null && results.length > 0){
        let login_token = uuidv4()
        let update_token_sql = 'update admin set token = ? where id = ?'
        await db.async.query(update_token_sql, [login_token, results[0].id])

        const admin_info = results[0]
        admin_info.token = login_token
        admin_info.password = ''
        res.json({
            code: 200,
            msg: '登录成功',
            data: admin_info
        })
    } else {
        res.json({
            code: 500,
            msg: '登录失败'
        })
    }
}